package cn.net.zhenchan.utils;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class AESUtils {
	
	private static Logger logger = LoggerFactory.getLogger(AESUtils.class);
	
	public static String STR_KEY = "Red_#$%_169";
	
	public static String aes_encrypt(String content, String strKey) {
		try {
			byte[] keyBytes = Arrays.copyOf(strKey.getBytes("ASCII"), 16);

			SecretKey key = new SecretKeySpec(keyBytes, "AES");
			Cipher cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.ENCRYPT_MODE, key);

			byte[] cleartext = content.getBytes("UTF-8");
			byte[] ciphertextBytes = cipher.doFinal(cleartext);

			return new String(Hex.encodeHex(ciphertextBytes)).toUpperCase();

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String aes_decrypt(String encryptResult, String strKey) {
		try {
			byte[] keyBytes = Arrays.copyOf(strKey.getBytes("ASCII"), 16);

			SecretKey key = new SecretKeySpec(keyBytes, "AES");
			Cipher cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.DECRYPT_MODE, key);

			byte[] cleartext = Hex.decodeHex(encryptResult.toCharArray());
			byte[] ciphertextBytes = cipher.doFinal(cleartext);

			return new String(ciphertextBytes, "UTF-8");

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			logger.info("----->BadPaddingException = " + e.getMessage());
		} catch (DecoderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		Long time = System.currentTimeMillis();
		String torrentName = "223260";
		
		System.err.println(time);
		
		String content = time + "~" + torrentName;
		// 加密
		System.out.println("加密前：" + content);
		String encryptResult = aes_encrypt(content, STR_KEY);
		System.out.println("加密后：" + encryptResult);
		System.err.println(encryptResult.substring(encryptResult.length() - 6, encryptResult.length()));
		//时间 + 种子 + AES（时间+种子）后六位
//		String str = time + content + encryptResult.substring(encryptResult.length() - 6, encryptResult.length());
//		System.err.println("---->str = " + str);
//		String secondEncrypt = aes_encrypt(str, password);
//		System.err.println(secondEncrypt);
		// 解密
		String decryptResult = aes_decrypt(encryptResult, STR_KEY);
		System.out.println("解密后：" + decryptResult);

		// SELECT HEX(aes_encrypt("password", "0123456789012345"));
		// SELECT aes_decrypt(UNHEX("E75C7C56AFB3EA4360A9856456F1C8A2"),
		// "0123456789012345");
	}
}
